<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifyDate">
      <el-input v-model="dataForm.modifyDate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="应用名称" prop="appName">
      <el-input v-model="dataForm.appName" placeholder="应用名称"></el-input>
    </el-form-item>
    <el-form-item label="应用路径" prop="path">
      <el-input v-model="dataForm.path" placeholder="应用路径"></el-input>
    </el-form-item>
    <el-form-item label="APP版本" prop="version">
      <el-input v-model="dataForm.version" placeholder="APP版本"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createDate: '',
          modifyDate: '',
          appName: '',
          path: '',
          version: '',
          remark: ''
        },
        dataRule: {
          createDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifyDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          appName: [
            { required: true, message: '应用名称不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '应用路径不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: 'APP版本不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/st/stapp/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createDate = data.stApp.createDate
                this.dataForm.modifyDate = data.stApp.modifyDate
                this.dataForm.appName = data.stApp.appName
                this.dataForm.path = data.stApp.path
                this.dataForm.version = data.stApp.version
                this.dataForm.remark = data.stApp.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/st/stapp/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createDate': this.dataForm.createDate,
                'modifyDate': this.dataForm.modifyDate,
                'appName': this.dataForm.appName,
                'path': this.dataForm.path,
                'version': this.dataForm.version,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
